import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.ToolsApplication;
import com.example.demo.service.AreaPrice;
import com.example.demo.service.DeliveryPriceConfig;
import com.example.demo.service.DeliveryPriceService;
import com.example.demo.service.WarehouseArea;
import com.example.demo.test.Dest;
import com.example.demo.utils.BigDecimalUtils;
import com.example.demo.yjcx.CurrencyEnum;
import com.example.demo.yjcx.FeeItemTypeEnum;
import com.example.demo.yjcx.dao.*;
import com.example.demo.yjcx.service.BasePriceService;
import com.example.demo.yjcx.service.YJCalculateService;
import com.example.demo.zb.ExcelReader;
import com.example.demo.zb.GetZB;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author linjingze
 * @date 2023/12/21 4:52 PM
 */
@SpringBootTest(classes = ToolsApplication.class)
public class ToolsTestApplication {

    @Autowired
    private BasePriceService basePriceService;

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Autowired
    private AreaFeeItemMapper areaFeeItemMapper;

    @Autowired
    private WarehouseFeeRelMapper warehouseFeeRelMapper;

    @Autowired
    private YJCalculateService yjCalculateService;


    @Test
    public void zb() throws IOException {
        GetZB.zb();
    }

    @Test
    public void test() throws IOException {
        ExcelReader.newkWeelyReport(new File("/Users/linjingze/Downloads/程乔玉工作/2024-07-12-095109-763096.xlsx"));
    }

    @Test
    public void search() {
        System.out.println(yjCalculateService.calculate("FTW2"));
    }

    @Test
    public void init() {
        NYC();
        CHI();
        HOU();
    }
    public void NYC() {
        String area = "NYC";
        String fileName = "/Users/linjingze/Downloads/程乔玉工作/ck1price.xlsx";
        File file = new File(fileName);
        List<Dest> oldDataList = EasyExcel.read(file).head(Dest.class).sheet().doReadSync();
        for (Dest dest : oldDataList) {
            String warehouse = dest.getWarehouse();
            BigDecimal a = dest.getA();
            List<String> warehouseList = StrUtil.split(warehouse, "/");
            for (String s : warehouseList) {
                QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("name", s);
                Warehouse warehouseDO = warehouseMapper.selectOne(queryWrapper);
                if (warehouseDO == null) {
                    warehouseDO = new Warehouse(s);
                    warehouseMapper.insert(warehouseDO);
                }
                AreaFeeItem areaFeeItem = getAreaFeeItem(area, FeeItemTypeEnum.CK, CurrencyEnum.USD, a, "派送成本 按托盘");
                areaFeeItemMapper.insert(areaFeeItem);
                WarehouseFeeRel warehouseFeeRel = new WarehouseFeeRel();
                warehouseFeeRel.setWarehouseId(warehouseDO.getId());
                warehouseFeeRel.setFeeItemId(areaFeeItem.getId());
                warehouseFeeRelMapper.insert(warehouseFeeRel);
            }
        }
        addDbJ(area);

        List<AreaFeeItem> feeItemList = new ArrayList<>();
        AreaFeeItem a1 = getAreaFeeItem(area, FeeItemTypeEnum.QYG, CurrencyEnum.CNY, new BigDecimal(2000), "码头费：THC/ORC");
        AreaFeeItem a2 = getAreaFeeItem(area, FeeItemTypeEnum.QYG, CurrencyEnum.CNY, new BigDecimal(400), "保险费");
        AreaFeeItem a3 = getAreaFeeItem(area, FeeItemTypeEnum.QYG, CurrencyEnum.CNY, new BigDecimal(3300), "装柜费");
        AreaFeeItem a4 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(40), "托架费Chassis Rental");
        AreaFeeItem a5 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(180), "清关费费，按进口商收");
        AreaFeeItem a6 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(450), "拖车费");
        AreaFeeItem a7 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(400), "拆柜费");
        AreaFeeItem a8 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(14), "打托费");
        AreaFeeItem a9 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(300), "渠道利润");
        feeItemList.add(a1);
        feeItemList.add(a2);
        feeItemList.add(a3);
        feeItemList.add(a4);
        feeItemList.add(a5);
        feeItemList.add(a6);
        feeItemList.add(a7);
        feeItemList.add(a8);
        feeItemList.add(a9);
        feeItemList.forEach(s -> areaFeeItemMapper.insert(s));
    }

    private void addDbJ(String area) {
        String areaName = DeliveryPriceConfig.nameMap.get(area);

        DeliveryPriceService service = new DeliveryPriceService();
        for (WarehouseArea warehouseArea : DeliveryPriceConfig.getAreaWarehouseSellMap().get(areaName).getWarehouseAreaList()) {
            String warehouse = warehouseArea.getWarehouse();
            QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", warehouse);
            Warehouse warehouseDO = warehouseMapper.selectOne(queryWrapper);
            if (warehouseDO != null) {
                System.out.println("已经存在跳过" + warehouse);
                continue;
            }
            warehouseDO = new Warehouse(warehouse);
            warehouseMapper.insert(warehouseDO);
            BigDecimal deliveryPrice = service.getDeliveryPrice(areaName, warehouse, 6);
            AreaFeeItem areaFeeItem = getAreaFeeItem(area, FeeItemTypeEnum.CK, CurrencyEnum.USD, BigDecimalUtils.div(deliveryPrice,new BigDecimal(6)), "单板价");
            areaFeeItemMapper.insert(areaFeeItem);
            WarehouseFeeRel warehouseFeeRel = new WarehouseFeeRel();
            warehouseFeeRel.setWarehouseId(warehouseDO.getId());
            warehouseFeeRel.setFeeItemId(areaFeeItem.getId());
            warehouseFeeRelMapper.insert(warehouseFeeRel);
        }
    }

    public void CHI() {
        String area = "CHI";
        String fileName = "/Users/linjingze/Downloads/程乔玉工作/chi.xlsx";
        File file = new File(fileName);
        List<Dest> oldDataList = EasyExcel.read(file).head(Dest.class).sheet().doReadSync();
        for (Dest dest : oldDataList) {
            String warehouse = dest.getWarehouse();
            BigDecimal a = dest.getA();
            List<String> warehouseList = StrUtil.split(warehouse, "/");
            for (String s : warehouseList) {
                QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("name", s);
                Warehouse warehouseDO = warehouseMapper.selectOne(queryWrapper);
                if (warehouseDO == null) {
                    warehouseDO = new Warehouse(s);
                    warehouseMapper.insert(warehouseDO);
                }
                AreaFeeItem areaFeeItem = getAreaFeeItem(area, FeeItemTypeEnum.CK, CurrencyEnum.USD, a, "派送成本 按托盘");
                areaFeeItemMapper.insert(areaFeeItem);
                WarehouseFeeRel warehouseFeeRel = new WarehouseFeeRel();
                warehouseFeeRel.setWarehouseId(warehouseDO.getId());
                warehouseFeeRel.setFeeItemId(areaFeeItem.getId());
                warehouseFeeRelMapper.insert(warehouseFeeRel);
            }
        }
        addDbJ(area);
        List<AreaFeeItem> feeItemList = new ArrayList<>();
        AreaFeeItem a1 = getAreaFeeItem(area, FeeItemTypeEnum.QYG, CurrencyEnum.CNY, new BigDecimal(2000), "码头费：THC/ORC");
        AreaFeeItem a2 = getAreaFeeItem(area, FeeItemTypeEnum.QYG, CurrencyEnum.CNY, new BigDecimal(400), "保险费");
        AreaFeeItem a3 = getAreaFeeItem(area, FeeItemTypeEnum.QYG, CurrencyEnum.CNY, new BigDecimal(3300), "装柜费");
        AreaFeeItem a4 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(40), "托架费Chassis Rental");
        AreaFeeItem a5 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(180), "清关费费，按进口商收");
        AreaFeeItem a6 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(1750), "拖车费");
        AreaFeeItem a9 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(300), "渠道利润");
        feeItemList.add(a1);
        feeItemList.add(a2);
        feeItemList.add(a3);
        feeItemList.add(a4);
        feeItemList.add(a5);
        feeItemList.add(a6);
        feeItemList.add(a9);
        feeItemList.forEach(s -> areaFeeItemMapper.insert(s));
    }

    public void HOU() {
        String area = "HOU";
        String fileName = "/Users/linjingze/Downloads/程乔玉工作/HOU.xlsx";
        File file = new File(fileName);
        List<Dest> oldDataList = EasyExcel.read(file).head(Dest.class).sheet().doReadSync();
        for (Dest dest : oldDataList) {
            String warehouse = dest.getWarehouse();
            BigDecimal a = dest.getA();
            List<String> warehouseList = StrUtil.split(warehouse, "/");
            for (String s : warehouseList) {
                QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("name", s);
                Warehouse warehouseDO = warehouseMapper.selectOne(queryWrapper);
                if (warehouseDO == null) {
                    warehouseDO = new Warehouse(s);
                    warehouseMapper.insert(warehouseDO);
                }
                AreaFeeItem areaFeeItem = getAreaFeeItem(area, FeeItemTypeEnum.CK, CurrencyEnum.USD, a, "派送成本 按托盘");
                areaFeeItemMapper.insert(areaFeeItem);
                WarehouseFeeRel warehouseFeeRel = new WarehouseFeeRel();
                warehouseFeeRel.setWarehouseId(warehouseDO.getId());
                warehouseFeeRel.setFeeItemId(areaFeeItem.getId());
                warehouseFeeRelMapper.insert(warehouseFeeRel);
            }
        }
        addDbJ(area);
        List<AreaFeeItem> feeItemList = new ArrayList<>();
        AreaFeeItem a1 = getAreaFeeItem(area, FeeItemTypeEnum.QYG, CurrencyEnum.CNY, new BigDecimal(2000), "码头费：THC/ORC");
        AreaFeeItem a2 = getAreaFeeItem(area, FeeItemTypeEnum.QYG, CurrencyEnum.CNY, new BigDecimal(400), "保险费");
        AreaFeeItem a3 = getAreaFeeItem(area, FeeItemTypeEnum.QYG, CurrencyEnum.CNY, new BigDecimal(3300), "装柜费");
        AreaFeeItem a4 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(40), "托架费Chassis Rental");
        AreaFeeItem a5 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(180), "清关费费，按进口商收");
        AreaFeeItem a6 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(1600), "拖车费");
        AreaFeeItem a7 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(100), "拆柜费");
        AreaFeeItem a8 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(65), "码头杂费");
        AreaFeeItem a9 = getAreaFeeItem(area, FeeItemTypeEnum.MDG, CurrencyEnum.USD, new BigDecimal(300), "渠道利润");
        feeItemList.add(a1);
        feeItemList.add(a2);
        feeItemList.add(a3);
        feeItemList.add(a4);
        feeItemList.add(a5);
        feeItemList.add(a6);
        feeItemList.add(a7);
        feeItemList.add(a8);
        feeItemList.add(a9);
        feeItemList.forEach(s -> areaFeeItemMapper.insert(s));
    }

    private static AreaFeeItem getAreaFeeItem(String area, FeeItemTypeEnum feeItemType, CurrencyEnum currency, BigDecimal a, String feeName) {
        AreaFeeItem areaFeeItem = new AreaFeeItem();
        areaFeeItem.setArea(area);
        areaFeeItem.setType(feeItemType.name());
        areaFeeItem.setCurrency(currency.name());
        areaFeeItem.setPrice(BigDecimalUtils.toPlainString(a));
        areaFeeItem.setFeeName(feeName);
        return areaFeeItem;
    }
}
